﻿var mxPlugin = window.mxPlugin || {};

$(document).ready(function () {
    mxPlugin.async = {
        tid: null,
        pollData: [],

        init: function () {
        },

        post: function (action, payload, callback) {
            callback = callback || mxPlugin.async.asyncCallback;
            
            callback(null, { message: "Sending..." });
            $.ajax({
                url: action,
                type: "post",
                cache: false,
                data: payload,
                success: function (r, s) {
                    mxPlugin.async.pollData.push({ cid: r, callback: callback });
                    mxPlugin.async.beginPoll();
                },
                error: function (r, s) {
                    callback(false, { message: "Sending data failed" });
                }
            });
        },

        beginPoll: function () {
            if (mxPlugin.async.tid || mxPlugin.async.pollData.length == 0)
                return;

            mxPlugin.async.tid = setInterval(mxPlugin.async.checkResults, 1000);
        },

        postDialog: function (source, descriptor, asyncCallback) {
            var form = $("form", source);
            var action = form.attr("action");
            var json = {};
            $("input", form).each(function (i, o) {
                if ($(o).attr("id")) {
                    eval("json." + $(o).attr("id") + "= \"" + $(o).val() + "\"");
                }
            });

            mxPlugin.async.post(action, json, asyncCallback);
        },

        checkResults: function () {
            $(mxPlugin.async.pollData).each(function (i, d) {

                var callback = d.callback || mxPlugin.async.asyncCallback;

                $.ajax({
                    url: "/Event/Get?correlationId=" + d.cid,
                    type: "post",
                    cache: false,
                    async: false,
                    dataType: "json",
                    success: function (r, s) {
                        switch (r.State) {
                            case 0:
                                d.message = "Unknown event id";
                                callback(false, d);
                                d.completed = true;
                                break;
                            case 1:
                                d.message = "Processing";
                                callback(null, d);
                                break;
                            case 2:
                                // Success
                                d.message = "Success";
                                callback(true, d);
                                d.completed = true;
                                break;
                            case 3:
                                d.message = "Error: " + r.Message;
                                callback(false, d);
                                d.completed = true;
                                break;
                        }
                    }
                });
            });

            for (var i = mxPlugin.async.pollData.length - 1; i >= 0; i--) {
                if (mxPlugin.async.pollData[i].completed)
                    mxPlugin.async.pollData.splice(i, 1);
            }

            if (mxPlugin.async.pollData.length == 0) {
                clearInterval(mxPlugin.async.tid);
                mxPlugin.async.tid = null;
            }
        },

        asyncCallback: function (success, descriptor) {
            var msg = $("[mx-asyncResult]");
            msg.show();
            msg.html(descriptor.message);
            if (success) {
                mxPlugin.dynamic.reloadAll();
                msg.fadeOut("slow");
            }
        }

    };

    mxPlugin.dynamic.init();
});